﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
namespace _5ChickDeal.Admin.Control
{
    public partial class EmailList : System.Web.UI.UserControl
    {
        protected void DropDownBind()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            var source = from tt in db.TinhThanhs
                         select tt;
            try
            {
                List<TinhThanh> tinhthanhs = source.ToList();
                tinhthanhs.Insert(0, new TinhThanh() { id = -1, ten = "Toàn quốc" });
                DropDownListTinhThanh.DataTextField = "ten";
                DropDownListTinhThanh.DataValueField = "id";
                DropDownListTinhThanh.DataSource = tinhthanhs;
                DropDownListTinhThanh.DataBind();
            }
            catch
            {
            }
        }

        protected void GridViewBind()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            int idtinh = int.Parse(DropDownListTinhThanh.SelectedValue);
            try
            {
                Object source;
                if (idtinh == -1)
                {
                    source = from email in db.EmailDangKies
                             select new { email.id, email.email, email.TinhThanh1.ten };
                }
                else
                {
                    source = from email in db.EmailDangKies
                             where email.tinhthanh == idtinh
                             select new { email.id, email.email, email.TinhThanh1.ten };
                }
                GridViewEmail.DataSource = source;
                GridViewEmail.DataBind();
            }
            catch
            {
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                DropDownBind();
                GridViewBind();
            }
        }

        protected void DropDownListTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewBind();
            PanelError.Visible = false;
            PanelOk.Visible = false;

        }



        protected void TimKiem_Click(object sender, EventArgs e)
        {
            if (TextBoxTimKiem.Text != "")
            {
                DataClassesDataContext db = new DataClassesDataContext();

                int idtinh = int.Parse(DropDownListTinhThanh.SelectedValue);
                try
                {
                    Object source;
                    if (idtinh == -1)
                    {
                        source = from email in db.EmailDangKies
                                 where email.email.Contains(TextBoxTimKiem.Text)
                                 select new { email.id, email.email, email.TinhThanh1.ten };
                    }
                    else
                    {
                        source = from email in db.EmailDangKies
                                 where email.tinhthanh == idtinh && email.email.Contains(TextBoxTimKiem.Text)
                                 select new { email.id, email.email, email.TinhThanh1.ten };
                    }
                    GridViewEmail.DataSource = source;
                    GridViewEmail.DataBind();
                }
                catch
                {
                }
            }
        }


        protected void GridViewEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewEmail.PageIndex = e.NewPageIndex;
            GridViewBind();
        }

        protected void GridViewEmail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    ImageButton imgbt = (ImageButton)e.Row.Cells[4].Controls[0];
                    imgbt.OnClientClick = "if(!confirm('Bạn thực sự muốn xóa?')) return false;";
                }
                catch
                {
                }
            }
        }

        protected void GridViewEmail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                DataClassesDataContext db = new DataClassesDataContext();
                TextBox textid = (TextBox)GridViewEmail.Rows[e.RowIndex].Cells[1].Controls[0];
                EmailDangKy em = db.EmailDangKies.Single(m => m.id == (int)GridViewEmail.DataKeys[e.RowIndex].Value);

                string current = em.email;
                em.email = textid.Text;
                db.SubmitChanges();
                LabelOk.Text = "Bạn đã sửa email " + current + " thành " + textid.Text;
                PanelOk.Visible = true;
                PanelError.Visible = false;
            }
            catch
            {
                label1.Text = "Cập nhật thất bại";
                PanelOk.Visible = false;
                PanelError.Visible = true;
            }
            GridViewEmail.EditIndex = -1;
            GridViewBind();
        }

        protected void GridViewEmail_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewEmail.EditIndex = e.NewEditIndex;

            GridViewBind();
            TextBox textid = (TextBox)GridViewEmail.Rows[e.NewEditIndex].Cells[0].Controls[0];
            textid.Enabled = false;
            TextBox textid2 = (TextBox)GridViewEmail.Rows[e.NewEditIndex].Cells[2].Controls[0];
            textid2.Enabled = false;
        }

        protected void GridViewEmail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {

                DataClassesDataContext db = new DataClassesDataContext();
                var email = db.EmailDangKies.Single(i => i.id == (int)this.GridViewEmail.DataKeys[e.RowIndex].Value);
                db.EmailDangKies.DeleteOnSubmit(email);
                db.SubmitChanges();
                Message(true, "Đã xóa email " + email.email + " tại thành phố " + email.TinhThanh1.ten);
                GridViewBind();

            }

            catch
            {
                Message(false, "Không xóa được");
            }
        }

        protected void GridViewEmail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewEmail.EditIndex = -1;
            GridViewBind();
        }

        private int GetMaxEmailID()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            int kq = 0;
            try
            {
                kq = db.EmailDangKies.Max(e => e.id);
            }
            catch { }
            return kq;
        }

        private void Message(bool success, String message)
        {
            if (success)
            {
                LabelOk.Text = message;

            }
            else
            {
                label1.Text = message;
            }
            PanelOk.Visible = success;
            PanelError.Visible = !success;
        }

        protected void Submit_Click(object sender, EventArgs e)
        {

            System.Text.RegularExpressions.Regex regEmail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (!regEmail.IsMatch(TextBoxEmail.Text))
            {
                Message(false, "Email bạn nhập không hợp lệ");
                return;
            }



            DataClassesDataContext db = new DataClassesDataContext();
            int id = int.Parse(DropDownListTinhThanh.SelectedValue);
            if (id == -1)
            {
                Message(false, "Bạn phải chọn thành phố");
                return;

            }
            EmailDangKy check = null;
            try
            {
                check = db.EmailDangKies.ToList().Find(c => c.email == TextBoxEmail.Text && c.tinhthanh == id);
            }
            catch
            {
                
            }
            if (check == null)
            {
                EmailDangKy email = new EmailDangKy();
                email.id = GetMaxEmailID() + 1;
                email.email = TextBoxEmail.Text;
                email.tinhthanh = id;
                db.EmailDangKies.InsertOnSubmit(email);
                db.SubmitChanges();
                GridViewBind();

                Message(true, "Đã thêm " + email.email);
            }
            else
            {
                Message(false, "Email này đã được đăng ký cho tỉnh (thành phố) này rồi!");
            }

        }
    }
}